package me.maxwin.view;

import java.util.zip.Inflater;

import com.example.lightble.R;


import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class SnoSeekBarDialog extends AlertDialog implements OnClickListener{

	SeekBar mSeekbar;
	Context mContext;
	private int mResID;
	private final int MAX_NUM=255;
	private final int MIN_NUM=1;
	
	public SnoSeekBarDialog(Context context,int resId) {
		super(context);
		// TODO Auto-generated constructor stub
		mContext=context;
		initView();
		mResID=resId;
	}
	/*
	 *接口回調  
	 *控件 秒数
	 */
	public interface OnSeekBarChangedListener {
		void changed(AlertDialog dialog,int resId,int progress);
	}
	public void setOnSeekBarChangedListener(OnSeekBarChangedListener callBack) {
		mOnSeekBarChangedListener = callBack;
	}
	private OnSeekBarChangedListener mOnSeekBarChangedListener;
	//------------------------
	public interface OnOkPressedListener {
		void changed(AlertDialog dialog,int resId,int progress);
	}
	public void setOnOkPressedListener(OnOkPressedListener callBack) {
		mOnOkPressedListener = callBack;
	}
	private OnOkPressedListener mOnOkPressedListener;
	
	private int mProgress=MIN_NUM;
	private void initView()
	{
		  LayoutInflater inflater =getLayoutInflater();
		  mSeekbar=(SeekBar)inflater.inflate(R.layout.seekbar_layout, null);
		
		  mSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				// TODO Auto-generated method stub
				String str="间隔:";
				float second=(float)(progress+MIN_NUM);
				float second2=second/2;
				str+=String.format("%.1f",second2);
				str+="秒";
				SnoSeekBarDialog.this.setTitle(str);
				mProgress=progress+MIN_NUM;
				if (mOnSeekBarChangedListener != null) {
						mOnSeekBarChangedListener.changed(SnoSeekBarDialog.this,mResID,progress+MIN_NUM);
				}
			}
		});
		  mSeekbar.setMax(MAX_NUM-MIN_NUM);
		  
		  setTitle("间隔0.5秒");
		setView(mSeekbar);  
		setButton("设定", this);
		setButton2("关闭", (OnClickListener) null);
	}

	@Override
	public void onClick(DialogInterface dialog, int which) {
		// TODO Auto-generated method stub
		if(mOnOkPressedListener!=null)
		{
			mOnOkPressedListener.changed(SnoSeekBarDialog.this,mResID,mProgress);
		}
	}
}
